﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    public class SoftwareEntry
    {
        public SoftwareEntry()
        {
        }
        /// <summary>
        /// 根据公司编号获取软件列表
        /// <creator>wangl</creator>
        /// <date>2013-09-10</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns>返回软件列表</returns>
        public DataSet GetListByFEntityNo(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  FSoftwareEntryID , ");
            strSql.Append(" FSoftwareCatalogID , ");
            strSql.Append(" FSoftwareName , ");
            strSql.Append(" FNote , ");
            strSql.Append(" FSoftwareSvrFileName , ");
            strSql.Append(" FSize , ");
            strSql.Append(" FPubDate , ");
            strSql.Append(" FDownloadCount , ");
            strSql.Append(" FPlatform , ");
            strSql.Append(" FDeveloper , ");
            strSql.Append(" FPubIp , ");
            strSql.Append(" FAttachFileExt , ");
            strSql.Append(" FAttachOldFileName , ");
            strSql.Append(" FPubEmpNo , ");
            strSql.Append(" FEntityNo ");
            strSql.Append(" FROM t_SoftwareEntry ");
            strSql.Append(" WHERE FEntityNo = @FEntityNo ");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FEntityNo",SqlDbType.NVarChar,12)
                                      };
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 根据公司编号和类别编号获取软件列表
        /// <creator>zhongyl</creator>
        /// <date>2013-09-10</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="fSoftwareCatalogID">类别编号</param>
        /// <param name="top">显示行数 0为全部</param>
        /// <returns>返回软件列表</returns>
        public DataSet GetListByFEntityNoAndFSoftwareCatalogID(string entityNo, string fSoftwareCatalogID,int top)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  ");
            if (top != 0)
            {
                strSql.Append(" top " + top);
            }
            strSql.Append(" FSoftwareEntryID , ");
            strSql.Append(" FSoftwareCatalogID , ");
            strSql.Append(" FSoftwareName , ");
            strSql.Append(" FNote , ");
            strSql.Append(" FSoftwareSvrFileName , ");
            strSql.Append(" FSize , ");
            strSql.Append(" FPubDate , ");
            strSql.Append(" FDownloadCount , ");
            strSql.Append(" FPlatform , ");
            strSql.Append(" FDeveloper , ");
            strSql.Append(" FPubIp , ");
            strSql.Append(" FAttachFileExt , ");
            strSql.Append(" FAttachOldFileName , ");
            strSql.Append(" FPubEmpNo , ");
            strSql.Append(" FEntityNo ");
            strSql.Append(" FROM t_SoftwareEntry ");
            strSql.Append(" WHERE FEntityNo = @FEntityNo and FSoftwareCatalogID=@FSoftwareCatalogID");
            SqlParameter[] parameters = { 
                        new SqlParameter("@FEntityNo",SqlDbType.NVarChar,12),
                        new SqlParameter("@FSoftwareCatalogID",SqlDbType.Decimal,13)
                                      };
            parameters[0].Value = entityNo;
            parameters[1].Value = fSoftwareCatalogID;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新下载次数
        /// <creator>wangl</creator>
        /// <date>2013-09-10</date>
        /// </summary>
        public void UpdateDownCount(decimal softwareEntryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_SoftwareEntry set ");
            strSql.Append("FDownloadCount=FDownloadCount+1 ");
            strSql.Append(" where FSoftwareEntryID=@FSoftwareEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FSoftwareEntryID", SqlDbType.Decimal,9)};
            parameters[0].Value = softwareEntryID;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>sunlj</creator>
        /// <date>2013-07-19</date>
        /// </summary>
        /// <param name="softwareEntryID">软件编号</param>
        /// <returns>返回软件对象实体</returns>
        public Model.SoftwareEntry GetModel(decimal softwareEntryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FSoftwareEntryID,FSoftwareCatalogID,FSoftwareName,FNote,FSoftwareSvrFileName,FSize,FPubDate,FDownloadCount,FPlatform,FDeveloper,FPubIp,FAttachFileExt,FAttachOldFileName,FPubEmpNo,FEntityNo from t_SoftwareEntry ");
            strSql.Append(" where FSoftwareEntryID=@FSoftwareEntryID");
            SqlParameter[] parameters = {
					new SqlParameter("@FSoftwareEntryID", SqlDbType.Decimal)
			};
            parameters[0].Value = softwareEntryID;

            Model.SoftwareEntry model = new Model.SoftwareEntry();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2013-09-10</date>
        /// </summary>
        public Model.SoftwareEntry DataRowToModel(DataRow row)
        {
            Model.SoftwareEntry model = new Model.SoftwareEntry();
            if (row != null)
            {
                if (row["FSoftwareEntryID"] != null && row["FSoftwareEntryID"].ToString() != "")
                {
                    model.FSoftwareEntryID = decimal.Parse(row["FSoftwareEntryID"].ToString());
                }
                if (row["FSoftwareCatalogID"] != null && row["FSoftwareCatalogID"].ToString() != "")
                {
                    model.FSoftwareCatalogID = decimal.Parse(row["FSoftwareCatalogID"].ToString());
                }
                if (row["FSoftwareName"] != null)
                {
                    model.FSoftwareName = row["FSoftwareName"].ToString();
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FSoftwareSvrFileName"] != null)
                {
                    model.FSoftwareSvrFileName = row["FSoftwareSvrFileName"].ToString();
                }
                if (row["FSize"] != null && row["FSize"].ToString() != "")
                {
                    model.FSize = decimal.Parse(row["FSize"].ToString());
                }
                if (row["FPubDate"] != null && row["FPubDate"].ToString() != "")
                {
                    model.FPubDate = DateTime.Parse(row["FPubDate"].ToString());
                }
                if (row["FDownloadCount"] != null && row["FDownloadCount"].ToString() != "")
                {
                    model.FDownloadCount = int.Parse(row["FDownloadCount"].ToString());
                }
                if (row["FPlatform"] != null)
                {
                    model.FPlatform = row["FPlatform"].ToString();
                }
                if (row["FDeveloper"] != null)
                {
                    model.FDeveloper = row["FDeveloper"].ToString();
                }
                if (row["FPubIp"] != null)
                {
                    model.FPubIp = row["FPubIp"].ToString();
                }
                if (row["FAttachFileExt"] != null)
                {
                    model.FAttachFileExt = row["FAttachFileExt"].ToString();
                }
                if (row["FAttachOldFileName"] != null)
                {
                    model.FAttachOldFileName = row["FAttachOldFileName"].ToString();
                }
                if (row["FPubEmpNo"] != null)
                {
                    model.FPubEmpNo = row["FPubEmpNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
            }
            return model;
        }

    }
}
